<template>
	<view>
		<view class="book_info">
			<view class="book_img">
				<image style="width: 80px;height: 100px;" :src="bookData.imgUrl" />
			</view>
			<view class="book_text_list">
				<view class="book_name">
					<text>{{ bookData.bookName }}</text>
				</view>
				<view class="book_author">
					<text>{{ bookData.author }}</text>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<text>{{ bookData.bookStatus }}</text>
				</view>
				<view>
					<text>{{ bookData.bookUpdateTime }}</text>
				</view>
				<view>
					<text>最新：{{ bookData.bookLastChapter }}</text>
				</view>
			</view>
		</view>
		<view>
			<view class="blue_view">
				<text>| 简介</text>
			</view>
			<view class="book_profile">
				<text>{{ bookData.bookProfile }}</text>
			</view>
		</view>
		<view>
			<view class="blue_view">
				<text>| 章节</text>
			</view>
			<view v-for="(item,index) in chapters" :key="index" class="chapter_list" @click="gotoBookContent(item.chapterUrl)">
				<text style="margin-left: 10rpx;">{{ item.chapterName }}</text>
			</view>
		</view>
		<view class="bottom_but">
			<view class="in_shelf" @click="addShelf">
				<text>{{ shelfText }}</text>
			</view>
			<view class="start_read" @click="startRead">
				<text>开始阅读</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bookWebUrl: '', // 书籍在网络上的地址
				bookData: {}, // 书籍基础信息
				chapters: [], // 章节列表数据
				shelfText: '加入书架', // 书架文字
				isShelfClick: true, // 加入书架按钮是否可以点击
				isLogin: false // 是否登录
			}
		},
		onLoad(options) {
			this.bookWebUrl = options.bookWebUrl;
			
			// 判断是否登录
			this.checkIsLogin();
			
			// 通过网络地址查询小说详情以及目录
			this.getBookDetailAndChapters();
		},
		methods: {
			// 判断是否登录
			checkIsLogin() {
				const token = uni.getStorageSync('token');
				if (!token) {
					this.isLogin = false;
					return;
				} else {
					this.isLogin = true;
					return;
				}
			},
			// 通过网络地址查询小说详情以及目录
			getBookDetailAndChapters() {
				this.bookData = {};
				this.chapters = [];
				uni.showLoading({
					title: '查询中,请稍候...'
				});
				this.$api.get({
					url: '/app/bqg/bookInfoAndChapters',
					params: {
						bookUrl: this.bookWebUrl,
						isLogin: this.isLogin
					}
				}).then(res => {
					// console.log('res', res);
					if (res && res.data.code === 0) {
						this.bookData = res.data.bookInfo;
						this.chapters = res.data.bookInfo.bookChapterList;
						if (this.bookData.isShelf) {
							this.shelfText = '已在书架';
							this.isShelfClick = false;
						} else {
							this.shelfText = '加入书架';
							this.isShelfClick = true;
						}
					}
					uni.hideLoading();
				}).catch(error => {
					uni.hideLoading();
				});
			},
			// 通过章节的网络地址进入查看内容
			gotoBookContent(chapterUrl) {
				uni.navigateTo({
					url: '/pages/book/read?chapterUrl=' + chapterUrl + '&bookWebId=' + this.bookData.bookWebId
				});
			},
			// 加入书架
			addShelf() {
				if (this.isShelfClick) {
					uni.showLoading({
						title: '正在加入书架,请稍候...'
					});
					this.bookData.bookWebUrl = this.bookWebUrl;
					this.bookData.bookChapterList = this.chapters;
					this.$api.post({
						url: '/app/bqg/joinShelf',
						params: this.bookData
					}).then(res => {
						// console.log('res', res);
						if (res && res.data.code === 0) {
							if (res.data.isSuccess === 'OK') {
								uni.showToast({
									title: '加入成功!',
									icon: 'success',
									duration: 2000
								});
								this.getBookDetailAndChapters();
							} else {
								uni.showToast({
									title: '加入失败!',
									icon: 'error',
									duration: 2000
								})
							}
						}
						uni.hideLoading();
					}).catch(error => {
						console.log('error', error);
						uni.hideLoading();
					})
				}
			},
			// 开始阅读
			startRead() {
				const chapterUrl = this.chapters[0].chapterUrl;
				uni.navigateTo({
					url: '/pages/book/read?chapterUrl=' + chapterUrl + '&bookWebId=' + this.bookData.bookWebId
				});
			}
		} 
	}
</script>

<style lang="scss">
	.book_info {
		display: flex;
		// align-items: center;
		margin-bottom: 10rpx;
		background: #fef9ef;
		
		.book_img {
			margin-left: 10rpx;
			margin-top: 10rpx;
		}
		
		.book_text_list {
			margin-left: 10rpx;
			margin-right: 10rpx;
			font-size: 0.8rem;
			
			.book_name {}
			.book_author {}
			.book_profile {}
		}
	}
	
	.chapter_list {
		display: flex;
		width: 100%;
		height: 50rpx;
		align-items: center;
		border-bottom: 1px solid #d8d9da;
		font-size: 0.8rem;
	}
	
	.blue_view {
		color: blue;
		font-weight: bold;
		font-size: 0.9rem;
		margin-left: 10rpx;
		margin-top: 15rpx;
	}
	.book_profile {
		font-size: 0.8rem;
		margin-left: 10rpx;
		margin-right: 10rpx;
	}
	
	.bottom_but {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		display: flex;
		
		
		.in_shelf {
			width: 50%;
			height: 70rpx;
			font-size: 0.8rem;
			background: #f6f8fa;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #828182;
		}
		.start_read {
			width: 50%;
			height: 70rpx;
			font-size: 0.8rem;
			background: red;
			color: #ffffff;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>